<template>
  <div class="maintenanceType">
    <el-row>
      <el-col :span="24">
        <dynForm :info-form="searchForm" :search-tags="searchTags">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}</el-button
            >
        </dynForm>
    </el-col></el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="newAdd"
      >{{ $t("add") }}</el-button
      >
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="modify"
      >
        {{ $t("modify") }}</el-button
        >
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="doDelete"
      >{{ $t("delete") }}</el-button
      >
    </div>
    <!-- 表单部分 -->
    <el-card shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <!--分页组件-->
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <handleForm ref="handleForm" @initList="initList"/>
  </div>
</template>

<script>
import mySetting from '@/mixins/mySetting'
import networkTips from '@/mixins/networkTips'
import { betterTips } from '@/utils/notification'
import handleForm from './components/handleForm'
import typeManageApi from '@/api/maintenance/typeManage'
export default {
  name: 'MaintenanceType',
  components: { handleForm },
  mixins: [mySetting, networkTips],
  props: {},
  data() {
    return {
      searchForm: {},
      searchTags: [
        { label: this.$t('pmTypeName'), prop: 'pmTypeName', tag: 'DynInput' }
      ],
      selectList: {},
      trackinTime: null,
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        { prop: 'pmTypeName', label: this.$t('pmTypeName'), width: '120px' },
        { prop: 'frequence', label: this.$t('frequence'), width: '120px' },
        { prop: 'frequenceNnit', label: this.$t('frequenceNnit'), width: '120px', templete: (_scope) => {
          return (
            <span>{ this.fliterFrequence(_scope.row.frequenceNnit)}</span>
          )
        } },
        { prop: 'remark', label: this.$t('remark'), width: '180px' },
        { prop: 'status', label: this.$t('status'), templete: (_scope) => {
          return (
            <el-switch
              v-model={_scope.row.status}
              active-value={1}
              inactive-value={0}
              active-color={'#13ce66'}
              inactive-color={'#ff4949'}
              disabled
            />
          )
        } },
        { prop: 'creator', label: this.$t('creator'), width: '120px' },
        { prop: 'createTime', label: this.$t('createTime'), width: '200px' },
        { prop: 'editor', label: this.$t('editor') },
        { prop: 'editTime', label: this.$t('editTime'), width: '200px' }
      ],
      tableLoading: true
    }
  },
  computed: {},
  watch: {},
  created() {
    this.initList()
    this.getFrequence()
    this.getStatus()
  },
  mounted() {},
  methods: {
    async initList() {
      this.searchForm = Object.assign({}, this.searchForm, this.handleTime('startTime', 'endTime', this.trackinTime))
      const res = await typeManageApi.getList(this.$clearNull({ ...this.searchForm, ...this.listQuery }))
      this.tableData = res.data
      this.total = res.total
      this.tableLoading = false
    },
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    newAdd() {
      this.$refs.handleForm.handleOpen()
    },
    modify() {
      this.$refs.handleForm.handleOpen(this.multipleSelection[0])
    },
    // 删除
    doDelete() {
      this.$confirm(
        `${this.$t('confirmDelete')}${this.multipleSelection.length}${this.$t(
          'recordCount'
        )}?`,
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map(ele => ele.id)
        typeManageApi.doDelete({ ids }).then(res => {
          betterTips(res, this.setOnePage, this.initList)
        })
      })
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    fliterFrequence(val) {
      const statusMap = {
        1: '天',
        2: '周',
        3: '月'
      }
      return statusMap[val]
    },
    // 获取频率下拉框
    async getFrequence() {
      const res = await typeManageApi.getFrequence()
      this.$set(this.selectList, 'frequence', res.data)
    },
    async getStatus() {
      const res = await typeManageApi.getStatus()
      this.$set(this.selectList, 'status', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
::v-deep .el-row:nth-child(1) {
  padding-bottom: 0;
}
</style>
